package com.atguigu.userprofile.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.userprofile.bean.TagCondition;
import com.atguigu.userprofile.bean.TaskInfo;
import com.atguigu.userprofile.bean.UserGroup;
import com.atguigu.userprofile.service.UserGroupService;
import com.atguigu.userprofile.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangchen
 * @since 2021-05-04
 */
@RestController
public class UserGroupController {

    @Autowired
    UserGroupService userGroupService;

    @RequestMapping("/user-group-list")
    @CrossOrigin
    public String  getUserGroupList(@RequestParam("pageNo")int pageNo , @RequestParam("pageSize") int pageSize){
        int startNo=(  pageNo-1)* pageSize;
        int endNo=startNo+pageSize;

        QueryWrapper<UserGroup> queryWrapper = new QueryWrapper<>();
        int count = userGroupService.count(queryWrapper);

        queryWrapper.orderByDesc("id").last(" limit " + startNo + "," + endNo);
        List<UserGroup> userGroupList = userGroupService.list(queryWrapper);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("detail",userGroupList);
        jsonObject.put("total",count);

        return  jsonObject.toJSONString();
    }


    /*
            处理前台的用户分群计算请求
                传入的是json

           UserGroup(id=null, userGroupName=90后男性和女性, conditionJsonStr=null,
           tagConditions=[TagCondition(tagCode=TAG_PERSON_NATURE_GENDER, tagName=性别, operatorName=包含,
           operator=in, tagValues=[男性, 女性],
           tagCodePath=[TAG_PERSON, TAG_PERSON_NATURE, TAG_PERSON_NATURE_GENDER]),
            TagCondition(tagCode=TAG_PERSON_NATURE_PERIOD, tagName=年代, operatorName=等于, operator=eq, tagValues=[90后], tagCodePath=[TAG_PERSON, TAG_PERSON_NATURE, TAG_PERSON_NATURE_PERIOD]),
             TagCondition(tagCode=TAG_CONSUME_BEHAIVOR_ORDER_AMOUNT_7d, tagName=最近7日下单金额, operatorName=大于, operator=gt, tagValues=[200], tagCodePath=[TAG_CONSUME_BEHAIVOR, TAG_CONSUME_BEHAIVOR_ORDER, TAG_CONSUME_BEHAIVOR_ORDER_AMOUNT_7d])], conditionComment=null, userGroupNum=null, updateType=MANUAL, userGroupComment=90后男性和女性, updateTime=null, createTime=null, busiDate=2020-06-14)

     */
    @PostMapping("/user-group")
    public String CrowdSegmentation(@RequestBody UserGroup userGroup){

        // 1.把分群信息存入Mysql。 把条件信息，转为json，调用 UserGroupMapper 把 收到的 userGroup 存入Mysql
        userGroupService.saveUserGroupToMysql(userGroup);

        //2. 把分群信息写入 ck
        userGroupService.saveUserGroupToCK(userGroup);
        //手动模拟一个异常，让程序每次到这里就中止，不返回数据给前端

        //3. 把分群信息写入redis
        UserGroup ug = userGroupService.saveUserGroupToRedis(userGroup);

        //4.赋值create_time
        ug.setCreateTime(new Date());

        userGroupService.saveOrUpdate(ug);

        int i = 1/0;
        return "success";

    }


    /*
            预估分群人数
     */
    @PostMapping("/user-group-evaluate")
    public Long evaluateUserGroup(@RequestBody UserGroup userGroup){

        Long num = userGroupService.evaluateUserGroup(userGroup);

        return num;

    }

    /*
            处理更新人群包功能

            /user-group-refresh/6    ?   busiDate=2020-06-15
     */
    @PostMapping("/user-group-refresh/{id}")
    public String updateUserGroup(String busiDate,@PathVariable("id") String id){

        //更新ck
        UserGroup userGroup = userGroupService.updateUserGroupInCk(busiDate, id);

        //更新redis
        UserGroup ug = userGroupService.saveUserGroupToRedis(userGroup);

        //生产一个更新完成的时间，更新mysql
        ug.setUpdateTime(new Date());

        userGroupService.saveOrUpdate(ug);

        return "success";
    }



}

